//
//  RONGL.m
//  RONH
//
//  Created by CHUN on 16/1/29.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "RONGL.h"
#import "ECDeviceHeaders.h"
#import "LoginViewController.h"
#import "MainViewController.h"
#import "DemoGlobalClass.h"
#import "AddressBookManager.h"
#import "CustomEmojiView.h"

@interface RONGL ()
{
    UITextField * _userName;
}
@property (nonatomic, strong) LoginViewController *loginView;
@property (nonatomic, strong) MainViewController *mainView;


@end

@implementation RONGL

- (void)viewDidLoad {
    [super viewDidLoad];
    [self RONG_login];
}

- (void)RONG_login
{
    //是否有登录信息
    [DemoGlobalClass sharedInstance].isAutoLogin = [self getLoginInfo];
    if ([DemoGlobalClass sharedInstance].isAutoLogin) {
        //打开本地数据库
        [[DeviceDBHelper sharedInstance] openDataBasePath:[DemoGlobalClass sharedInstance].userName];
        self.mainView = [[MainViewController alloc] init];
        [self.navigationController pushViewController:self.mainView animated:YES];
        //        rootView = [[UINavigationController alloc] initWithRootViewController:_mainView];
    } else {
        
        //        self.loginView = [[LoginViewController alloc] init];
        //       [self.navigationController pushViewController:self.loginView animated:YES];
        //        rootView = [[UINavigationController alloc] initWithRootViewController:_loginView];
        
        [self nextBtnClicked];
    }

}

-(BOOL)getLoginInfo {
    
    NSString *loginInfo = [DemoGlobalClass sharedInstance].userName;
    if (loginInfo.length>0) {
        return YES;
    }
    return NO;
}

#pragma mark -登陆-
-(void)nextBtnClicked {
    
    NSCharacterSet *ws = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    _userName = [[UITextField alloc]init];
    _userName.text = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:User_id]];
    NSString *trimmed = [_userName.text stringByTrimmingCharactersInSet:ws];
    
    //校验账号是否为手机号
    //    NSString* errormessage = [LoginViewController valiMobile:trimmed];
    
    if (trimmed.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"容联账号为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    if (trimmed.length>0)
    {
        [self.view endEditing:YES];
        
        [[NSUserDefaults standardUserDefaults] setObject:trimmed forKey:@"lasttimeuser"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        NSString *userName = trimmed;
        NSString *password = nil;
        //[_password.text stringByTrimmingCharactersInSet:ws];
        
        ECLoginInfo * loginInfo = [[ECLoginInfo alloc] init];
        loginInfo.username = userName;
        loginInfo.userPassword = password;
        loginInfo.appKey = [DemoGlobalClass sharedInstance].appKey;
        loginInfo.appToken = [DemoGlobalClass sharedInstance].appToken;
        loginInfo.authType = [DemoGlobalClass sharedInstance].loginAuthType;
        loginInfo.mode = LoginMode_InputPassword;
        
        __weak typeof(self) weakself = self;
        MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:weakself.view animated:YES];
        hud.labelText = @"正在登录...";
        hud.removeFromSuperViewOnHide = YES;
        
        [DemoGlobalClass sharedInstance].userPassword = password;
        [[DeviceDBHelper sharedInstance] openDataBasePath:userName];
        [DemoGlobalClass sharedInstance].isHiddenLoginError = NO;
        [[ECDevice sharedInstance] login:loginInfo completion:^(ECError *error){
            
            //            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onConnected object:error];
            //            if (error.errorCode == ECErrorType_NoError) {
            //                [DemoGlobalClass sharedInstance].userName = userName;
            //            }
            //
            //            __strong typeof(weakself) strongSelf = weakself;
            //            [MBProgressHUD hideHUDForView:strongSelf.view animated:YES];
            
            [self LoginSuccess];
            
        }];
    }
}

//登录成功，页面跳转
-(void)LoginSuccess {
    MainViewController * lsvc = [[MainViewController alloc]init];
    [self.navigationController pushViewController:lsvc animated:YES];
}























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
